//
//  XDCycleView.m
//  AliPay001
//
//  Created by lucy on 16/8/22.
//  Copyright © 2016年 com.huasheng. All rights reserved.
//

#import "XDCycleView.h"
#import "XDCycleFlowLayout.h"
#import "Masonry.h"
#import "XDCycleCell.h"

#define SectionCount 100

@interface XDCycleView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,weak)UICollectionView* collectionView;
@property(nonatomic,weak)UIPageControl* pageControl;
@property(nonatomic,strong)NSTimer* timer;//计时器要强引用

@end

static NSString* cycleCell=@"cycleCell";

@implementation XDCycleView




-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if(self)
    {
    
        [self setupUI];
    
    }

    return self;


}


//初始化界面
-(void)setupUI
{
    [self setupCollectionView];
    [self setupTimer];
    [self setupPageControl];




}


//设置CollectionView
-(void)setupCollectionView
{
    
    //布局
    XDCycleFlowLayout *layout=[[XDCycleFlowLayout alloc]init];
    
    UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.backgroundColor=[UIColor greenColor];
    [self addSubview:collectionView];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.offset(0);
        
        
    }];
    
    //添加代理方法
    collectionView.dataSource=self;
    collectionView.delegate=self;
    
    //注册cell
    [collectionView registerClass:[XDCycleCell class] forCellWithReuseIdentifier:cycleCell];
    
    collectionView.pagingEnabled=YES;
    collectionView.bounces=NO;
    collectionView.showsVerticalScrollIndicator=NO;
    collectionView.showsHorizontalScrollIndicator=NO;
    
    self.collectionView=collectionView;

}


//设置pageControl
-(void)setupPageControl
{
    UIPageControl *pageControl=[[UIPageControl alloc]init];
    pageControl.currentPageIndicatorTintColor=[UIColor lightGrayColor];
    pageControl.pageIndicatorTintColor=[UIColor darkGrayColor];
    
    [self addSubview:pageControl];
    
    pageControl.enabled=NO;//取消点击
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.offset(0);
        make.bottom.offset(10);
        
        
    }];
    
    self.pageControl=pageControl;



}


//添加定时器
-(void)setupTimer
{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
//    self.timer=[NSTimer timerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
//
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

}



-(void)nextPage
{

    NSInteger page=self.pageControl.currentPage;
    NSIndexPath *scrollPath=nil;
    
    if(page==_imageArr.count-1)
    {
        
        scrollPath=[NSIndexPath indexPathForItem:0 inSection:SectionCount/2+1];
    
    }

    else
    {
       scrollPath= [NSIndexPath indexPathForItem:page+1 inSection:SectionCount/2];
    
    }
    
    
    [self.collectionView scrollToItemAtIndexPath:scrollPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
    
}


//在有拖拽行为时定时器关闭
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.timer.fireDate=[NSDate distantFuture];



}


//拖拽结束以后可以开始滚动
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.timer.fireDate=[NSDate dateWithTimeIntervalSinceNow:2];


}

// 用代码动画方式滚动,滚动一页动画停下来后会调用此方法"主要判断如果走到非中间组就让它无动画效果的回到中间这一组"
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:scrollView];

}


// 此方法只有在手动拖拽时当滚动完全停下来才会调用一次的方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page=scrollView.contentOffset.x/scrollView.bounds.size.width;
    
    NSInteger sectionNum=page/_imageArr.count;
    NSInteger itemNum=page%_imageArr.count;
    
    if(sectionNum==SectionCount/2)
    {
        return ;
    
    }
    
    NSIndexPath* indexPath=[NSIndexPath indexPathForItem:itemNum inSection:SectionCount/2];
    
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    


}


//页数的计算
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page=scrollView.contentOffset.x/scrollView.bounds.size.width+0.499;
    NSInteger pageNO=page % _imageArr.count;

    self.pageControl.currentPage=pageNO;

}


//实现数据源方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return SectionCount;

}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArr.count;


}



//每个Cell里有什么
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XDCycleCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:cycleCell forIndexPath:indexPath];
    cell.image=_imageArr[indexPath.item];
    
    return cell;

}


//让定时器无效
-(void)removeFromSuperview
{
    [super removeFromSuperview];
    [self.timer invalidate];


}

//重写属性的setter方法
-(void)setImageArr:(NSArray *)imageArr
{
    _imageArr=imageArr;
    
    [self.collectionView reloadData];
    
    //确定点的总个数
    self.pageControl.numberOfPages=_imageArr.count;
    [self layoutIfNeeded];
    
    //确定中间第0个cell的索引
    
    NSIndexPath *scrollIndex=[NSIndexPath indexPathForItem:0 inSection:SectionCount/2];
    [self.collectionView scrollToItemAtIndexPath:scrollIndex atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
}

@end











